import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:pinch_zoom/pinch_zoom.dart';

import '../utils/const.dart';

class ImagePreview extends StatelessWidget {
  final List<String> list;
  final int? index;
  final VoidCallback? onClose;

  const ImagePreview({Key? key, required this.list, this.index, this.onClose}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onClose,
      child: Container(
        color: Colors.black,
        child: Stack(
          children: [
            PageView.builder(
              itemBuilder: (context, index) => _buildItem(index),
              itemCount: list.length,
              controller: PageController(initialPage: index == null ? 0 : index!),
            ),
            GestureDetector(
              onTap: onClose,
              child: const Padding(
                padding: EdgeInsets.all(10),
                child: Icon(
                  Icons.close,
                  color: Colors.white,
                ),
              ),
            )
          ],
        ),
      ),
    );
  }

  _buildItem(int index) {
    return PinchZoom(
      child: CachedNetworkImage(
        imageUrl: list[index],
        httpHeaders: Const.imgHeaders,
      ),
    );
  }
}
